<template>
	<view class="container">
		<!-- 排序和搜索区域 -->
		<view class="filter-section">
			<!-- 排序选项 -->
			<view class="sort-options">
				<text class="sort-label">排序:</text>
				<view class="sort-item" :class="{ active: currentSort.type === 'deadline' }"
					@click="changeSort('deadline')">
					截止日期
					<text class="sort-arrow" v-if="currentSort.type === 'deadline'">
						{{ currentSort.order === 'asc' ? '↑' : '↓' }}
					</text>
				</view>
				<view class="sort-item" :class="{ active: currentSort.type === 'quantity' }"
					@click="changeSort('quantity')">
					需求数量
					<text class="sort-arrow" v-if="currentSort.type === 'quantity'">
						{{ currentSort.order === 'asc' ? '↑' : '↓' }}
					</text>
				</view>
				<view class="sort-item" :class="{ active: currentSort.type === 'publish' }"
					@click="changeSort('publish')">
					最新发布
					<text class="sort-arrow" v-if="currentSort.type === 'publish'">
						{{ currentSort.order === 'asc' ? '↑' : '↓' }}
					</text>
				</view>
			</view>

			<!-- 搜索框 -->
			<view class="search-box">
				<view class="search-icon">🔍</view>
				<input class="search-input" placeholder="搜索商品型号或品牌..." v-model="searchKeyword" @confirm="handleSearch"
					placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 商品列表 -->
		<view class="product-list">
			<view class="product-card" v-for="(product, index) in products" :key="product.id">
				<view class="product-header">
					<text class="product-model">型号:{{ product.product_model }}</text>
					<text class="product-brand">品牌:{{ product.brand_name }}</text>
				</view>

				<view class="product-info">
					<view class="info-item">
						<text class="info-label">需求数量:</text>
						<text class="info-value">{{ product.required_quantity }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">截止:</text>
						<text class="info-value deadline">{{ product.deadline }}</text>
					</view>
				</view>

				<view class="product-remark">
					<text class="remark-label">备注:</text>
					<text class="remark-content">{{ product.note }}</text>
				</view>

				<view class="product-actions">
					<view class="quote-btn" @click="toDetail(product)">
						立即报价
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :content-text="contentText" />
		<!-- 加载中 -->
		<uv-loading-page :loading="loading" loading-text="加载中..." font-size="24rpx"
			bgColor="rgba(0, 0, 0, 0.3)"></uv-loading-page>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				currentSort: {
					type: 'deadline',
					order: 'desc' // 'asc' 升序, 'desc' 降序
				},
				searchKeyword: '',
				reload: true,
				products: [],
				currentPage: 1, //当前页码
				lastPage: 1, //总页码
				status: 'loading',
				contentText: {
					contentdown: ' ',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
			};
		},
		onLoad() {
			this.resetPage();
		},
		onPullDownRefresh() {
			this.resetPage();
		},
		onReachBottom() {
			if (this.currentPage >= this.lastPage) {
				this.status = 'noMore';
			} else {
				this.reload = false;
				this.currentPage = this.currentPage + 1; //页码+1
				this.status = 'loading';
				this.loadData();
			}
		},
		methods: {
			loadData() {
				this.loading = true;
				let type = 'deadline';
				if (this.currentSort.type === 'quantity') {
					type = 'required_quantity';
				} else if (this.currentSort.type === 'publish') {
					type = 'create_time';
				}
				uni.$uv.http.get('/inquiry/getAllInquiryGoodsList', {
					params: {
						page: this.currentPage,
						sort: type,
						order: this.currentSort.order,
						search: this.searchKeyword,
					}
				}).then(res => {
					const data = res.res.data;
					this.currentPage = data.current_page;
					this.lastPage = data.last_page;
					if (res.res.code == 1) {
						this.products = this.reload ? data.list : this.products.concat(data.list);
						this.currentPage = res.data.current_page;
						this.lastPage = res.data.last_page;
						this.status = 'more';
					} else {
						this.$wanlshop.msg(res.res.msg, 3000, true, 'none');
					}
					this.loading = false;
				}).catch(error => {
					this.$wanlshop.msg('获取失败，请稍后重试', 2000, true);
					this.loading = false;
				});
			},
			changeSort(sortType) {
				if (this.currentSort.type === sortType) {
					// 相同排序类型，切换排序方向
					this.currentSort.order = this.currentSort.order === 'asc' ? 'desc' : 'asc';
				} else {
					// 不同排序类型，设置默认排序方向
					this.currentSort = {
						type: sortType,
						order: 'asc'
					};
				}
				this.resetPage();
			},
			handleSearch() {
				this.resetPage();
			},
			resetPage() {
				this.reload = true;
				this.status = 'loading';
				this.currentPage = 1;
				this.loadData();
			},
			toDetail(product) {
				uni.showModal({
					title: '立即报价',
					content: `确定要为商品 ${product.product_model} 报价吗？`,
					success: (res) => {
						if (res.confirm) {
							this.$wanlshop.to("/pages/future/quote?info=" + JSON.stringify(
								product));
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 30rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
		box-sizing: border-box;
	}

	.filter-section {
		margin-bottom: 30rpx;
	}

	.sort-options {
		display: flex;
		align-items: center;
		margin-bottom: 24rpx;

		.sort-label {
			font-size: 28rpx;
			color: #666;
			margin-right: 20rpx;
		}

		.sort-item {
			display: flex;
			align-items: center;
			padding: 12rpx 24rpx;
			border: 1rpx solid #e0e0e0;
			border-radius: 6rpx;
			font-size: 26rpx;
			color: #666;
			margin-right: 16rpx;
			background-color: #fff;

			&.active {
				background-color: #fe6600;
				color: #fff;
				border-color: #fe6600;
			}

			.sort-arrow {
				margin-left: 8rpx;
				font-size: 20rpx;
			}
		}
	}

	.search-box {
		position: relative;
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 8rpx;
		padding: 16rpx 24rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

		.search-icon {
			margin-right: 16rpx;
			font-size: 28rpx;
			color: #999;
		}

		.search-input {
			flex: 1;
			font-size: 28rpx;
			color: #333;
		}
	}

	.product-list {
		.product-card {
			background-color: #fff;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 24rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.product-header {
				display: flex;
				justify-content: space-between;
				margin-bottom: 24rpx;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #f0f0f0;

				.product-model {
					font-size: 30rpx;
					font-weight: bold;
					color: #333;
				}

				.product-brand {
					font-size: 26rpx;
					color: #666;
				}
			}

			.product-info {
				display: flex;
				justify-content: space-between;
				margin-bottom: 20rpx;

				.info-item {
					display: flex;
					flex-direction: column;

					.info-label {
						font-size: 26rpx;
						color: #999;
						margin-bottom: 8rpx;
					}

					.info-value {
						font-size: 28rpx;
						color: #333;
						font-weight: 500;

						&.deadline {
							color: #fe6600;
							font-weight: bold;
						}
					}
				}
			}

			.product-remark {
				display: flex;
				margin-bottom: 30rpx;
				padding: 20rpx;
				background-color: #f9f9f9;
				border-radius: 8rpx;

				.remark-label {
					font-size: 26rpx;
					color: #999;
					margin-right: 16rpx;
					white-space: nowrap;
				}

				.remark-content {
					font-size: 26rpx;
					color: #666;
					line-height: 1.5;
				}
			}

			.product-actions {
				display: flex;
				justify-content: flex-end;

				.quote-btn {
					background-color: #fe6600;
					color: #fff;
					padding: 20rpx 40rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
					font-weight: 500;
				}
			}
		}
	}

	.placeholder {
		color: #ccc;
		font-size: 28rpx;
	}
</style>